document.addEventListener('DOMContentLoaded', () => {
  const speedControl = document.getElementById('speed-control');
  const speedValue = document.getElementById('speed-value');
  const progressControl = document.getElementById('progress-control');
  const progressValue = document.getElementById('progress-value');

  // 获取当前视频状态
  chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
    // 添加错误处理
    try {
      chrome.tabs.sendMessage(tabs[0].id, {type: 'GET_STATE'}, (response) => {
        if (chrome.runtime.lastError) {
          console.log('等待页面加载...');
          return;
        }
        if (response) {
          speedControl.value = response.speed;
          speedValue.textContent = response.speed.toFixed(1);
          progressControl.value = response.progress;
          progressValue.textContent = Math.round(response.progress);
        }
      });
    } catch (error) {
      console.log('连接失败:', error);
    }
  });

  // 速度控制
  speedControl.addEventListener('input', (e) => {
    const speed = parseFloat(e.target.value);
    speedValue.textContent = speed.toFixed(1);
    chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
      try {
        chrome.tabs.sendMessage(tabs[0].id, {
          type: 'SET_SPEED',
          speed: speed
        }, (response) => {
          if (chrome.runtime.lastError) {
            console.log('等待页面加载...');
          }
        });
      } catch (error) {
        console.log('设置速度失败:', error);
      }
    });
  });

  // 进度控制
  progressControl.addEventListener('input', (e) => {
    const progress = parseInt(e.target.value);
    progressValue.textContent = progress;
    chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
      try {
        chrome.tabs.sendMessage(tabs[0].id, {
          type: 'SET_PROGRESS',
          progress: progress
        }, (response) => {
          if (chrome.runtime.lastError) {
            console.log('等待页面加载...');
          }
        });
      } catch (error) {
        console.log('设置进度失败:', error);
      }
    });
  });
});

// 添加作者信息到右下角
function addAuthorInfo() {
  const authorDiv = document.createElement('div');
  authorDiv.style.cssText = `
    position: fixed;
    bottom: 10px;
    right: 10px;
    font-size: 12px;
    color: #666;
    font-family: Arial, sans-serif;
  `;
  authorDiv.textContent = 'bilibili关注作者：Alex独开';
  document.body.appendChild(authorDiv);
}

// 在页面加载完成后添加作者信息
document.addEventListener('DOMContentLoaded', addAuthorInfo); 